<?php namespace WoXuanWang\Video\Controllers;

use App\Http\Requests\IndexRequest;
use App\Http\Requests\ShowRequest;
use Illuminate\Http\JsonResponse;
use WoXuanWang\ActionLog\Repositories\ActionLogRepository;
use WoXuanWang\Video\Repositories\VideoCategoryRepository;
use WoXuanWang\Video\Requests\CreateVideoCategoryRequest;
use WoXuanWang\Video\Requests\UpdateVideoCategoryRequest;
use zgldh\Scaffold\AppBaseController;

class VideoCategoryController extends AppBaseController
{

    public function __construct(VideoCategoryRepository $videocategoryRepository)
    {
        $this->repository = $videocategoryRepository;
        $this->middleware("auth");
    }

    /**
     * Display a listing of the VideoCategory.
     *
     * @param Request $request
     * @return Response
     */
    public function index(IndexRequest $request)
    {
        $with = $request->getWith();
        $data = $this->repository->datatables(null, $with);
        return $data;
    }

    /**
     * Store a newly created VideoCategory in storage.
     *
     * @param CreateVideoCategoryRequest $request
     * @return JsonResponse
     */
    public function store(CreateVideoCategoryRequest $request)
    {
        $input = $request->all();

        $videocategory = $this->repository->create($input);
        ActionLogRepository::log("新增", "视频分类(VideoCategory):" . $videocategory->id, \Auth::user());

        return $this->sendResponse($videocategory, 'VideoCategory saved successfully.');
    }

    /**
     * Display the specified VideoCategory.
     *
     * @param  int $id
     *
     * @return JsonResponse
     */
    public function show($id, ShowRequest $request)
    {
        $this->repository->with($request->getWith());
        $videocategory = $this->repository->findWithoutFail($id);

        if (empty($videocategory)) {
            return $this->sendError('VideoCategory not found');
        }

        return $this->sendResponse($videocategory, '');
    }

    /**
     * Update the specified VideoCategory in storage.
     *
     * @param  int $id
     * @param UpdateVideoCategoryRequest $request
     *
     * @return JsonResponse
     */
    public function update($id, UpdateVideoCategoryRequest $request)
    {
        $videocategory = $this->repository->findWithoutFail($id);

        if (empty($videocategory)) {
            return $this->sendError('VideoCategory not found');
        }

        $videocategory = $this->repository->update($request->all(), $id);
        ActionLogRepository::log("编辑", "视频分类(VideoCategory):" . $videocategory->id, \Auth::user());

        return $this->sendResponse($videocategory, 'VideoCategory updated successfully.');
    }

    /**
     * Remove the specified VideoCategory from storage.
     *
     * @param  int $id
     *
     * @return JsonResponse
     */
    public function destroy($id)
    {
        $videocategory = $this->repository->findWithoutFail($id);

        if (empty($videocategory)) {
            return $this->sendError('VideoCategory not found');
        }

        $this->repository->delete($id);
        return $this->sendResponse($videocategory, 'VideoCategory deleted successfully.');
    }
}
